#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Normalize TANGL                                                    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 11/24/2016                                             #
# Last Modification: 11/24/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 70
#
# MANUAL: This script reads all TANGL values and compares them with the ones defiled as DEFOCUS_TANGL.
#
# MANUAL: It then calculates the average magnitude (i.e., sum(ABS(TANGL)) / N ) of all TANGLs and of all DEFOCUS_TANGLs, and then normalizes the TANGLE values so that their average is that of the other ones. 
#
# PUBLICATION: 3D reconstruction of two-dimensional crystals: <A HREF="http://www.ncbi.nlm.nih.gov/pubmed/26093179">Arch Biochem Biophys 581, 68-77 (2015)</A>
# PUBLICATION: 3D Reconstruction from 2D crystal image and diffraction data: <A HREF="http://dx.doi.org/10.1016/S0076-6879(10)82004-X">Methods Enzymol. 482, Chapter 4, 101-129 (2010)</A>
# PUBLICATION: 2dx - Automated 3D structure reconstruction from 2D crystal data: <A HREF="http://journals.cambridge.org/action/displayAbstract?aid=1943200">Microscopy and Microanalysis 14(Suppl. 2), 1290-1291 (2008)</A>
# PUBLICATION: 2dx_merge - Data management and merging for 2D crystal images: <A HREF="http://dx.doi.org/10.1016/j.jsb.2007.09.011">J. Struct. Biol. 160(3), 375-384 (2007)</A>
#
# DISPLAY: tempkeep
# DISPLAY: reftiltgeo
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set reftiltgeo = ""
#
#$end_vars
#
source ${proc_2dx}/initialize
#
set scriptname = "2dx_mergeNormalizeTANGL"
\rm -f LOGS/${scriptname}.results
#
if ( ${reftiltgeo} == "y" ) then
  #############################################################################
  ${proc_2dx}/linblock "normalize_TANGL.py - to maintain tilt angles at initial values."
  #############################################################################
  #
  set olddir = $PWD
  cd ..
  set basedir = $PWD
  cd ${olddir}
  #
  set outfile = SCRATCH/2dx_mergeNormalizeTANGL.results
  \rm -rf ${outfile}
  ${app_python} ${proc_2dx}/normalize_TANGL.py 2dx_merge_dirfile.dat ${outfile} ${basedir}
  #
  cat ${outfile} >> LOGS/${scriptname}.results
  \rm -f ${outfile}
  #
endif
#
#############################################################################
${proc_2dx}/linblock "${scriptname} normal end."
#############################################################################
#
echo "<<@progress: 100>>"
#
exit
# for GUI:
python ${proc_2dx}/normalize_TANGL.py 2dx_merge_dirfile.dat
